﻿using AntDesign;
using Microsoft.AspNetCore.Components;
using Volo.Abp.AspNetCore.Components.Progression;

namespace Allin.Abp.AntDesignUI.Components
{
    public partial class UiPageProgress : ComponentBase
    {

        protected double Percentage { get; set; }
        protected bool Visible { get; set; }
        protected Color Color { get; set; } = default!;

        [Inject] protected IUiPageProgressService? UiPageProgressService { get; set; }

        protected override void OnInitialized()
        {
            base.OnInitialized();

            if (UiPageProgressService != null)
            {
                UiPageProgressService.ProgressChanged += OnProgressChanged;
            }
        }

        private async void OnProgressChanged(object? sender, UiPageProgressEventArgs e)
        {
            Percentage =e.Percentage??-1;
            Visible = e.Percentage == null || (e.Percentage >= 0 && e.Percentage <= 100);
            Color = GetColor(e.Options.Type);//TODO 不知道何时使用

            await InvokeAsync(StateHasChanged);
        }

        protected virtual Color GetColor(UiPageProgressType pageProgressType)
        {
            //return pageProgressType switch
            //{
            //    UiPageProgressType.Info => Color.,
            //    UiPageProgressType.Success => Color.Success,
            //    UiPageProgressType.Warning => Color.Warning,
            //    UiPageProgressType.Error => Color.Danger,
            //    _ => Color.Default,
            //};
            return Color.Blue6;
        }
        public virtual void Dispose()
        {
            if (UiPageProgressService != null)
            {
                UiPageProgressService.ProgressChanged -= OnProgressChanged;
            }
        }

    }
}
